package com.wdh.kjs.picker;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.wdh.kjs.util.AssetsUtils;


import java.util.ArrayList;

/**
 * 获取地址数据并显示地址选择器
 * @version 2015/12/15
 */
public class AddressInitTask extends AsyncTask<String, Void, ArrayList<AddressPicker.Province>> {
    private Activity activity;
    private TextView sheng;
    private TextView shi;
    private TextView xian;
    private ProgressDialog dialog;
    private String selectedProvince = "", selectedCity = "", selectedCounty = "";
    private boolean hideCounty=false;
    private TextView rongzi_address_sheng_youbian;
    private TextView rongzi_address_shi_youbian;
    private TextView rongzi_address_xian_youbian;
    public AddressInitTask(Activity activity, TextView sheng, TextView shi, TextView xian, boolean hideCounty) {
        this.activity = activity;
        this.sheng = sheng;
        this.shi = shi;
        this.xian = xian;
        this.hideCounty=hideCounty;
        dialog = ProgressDialog.show(activity, null, "正在初始化数据...", true, true);
    }
    public AddressInitTask(Activity activity, TextView sheng,TextView shi,TextView xian,TextView rongzi_address_sheng_youbian,TextView rongzi_address_shi_youbian,TextView rongzi_address_xian_youbian) {
        this.activity = activity;
        this.sheng=sheng;
        this.shi=shi;
        this.xian=xian;
        this.rongzi_address_sheng_youbian=rongzi_address_sheng_youbian;
        this.rongzi_address_shi_youbian=rongzi_address_shi_youbian;
        this.rongzi_address_xian_youbian = rongzi_address_xian_youbian;
        dialog = ProgressDialog.show(activity, null, "正在初始化数据...", true, true);
    }

    @Override
    protected ArrayList<AddressPicker.Province> doInBackground(String... params) {
        if (params != null) {
            switch (params.length) {
                case 1:
                    selectedProvince = params[0];
                    break;
                case 2:
                    selectedProvince = params[0];
                    selectedCity = params[1];
                    break;
                case 3:
                    selectedProvince = params[0];
                    selectedCity = params[1];
                    selectedCounty = params[2];
                    break;
                default:
                    break;
            }
        }
        ArrayList<AddressPicker.Province> data = new ArrayList<AddressPicker.Province>();
        try {
            String json = AssetsUtils.readText(activity, "city.json");
            data.addAll(JSON.parseArray(json, AddressPicker.Province.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    @Override
    protected void onPostExecute(ArrayList<AddressPicker.Province> result) {
        dialog.dismiss();
        if (result.size() > 0) {
            AddressPicker picker = new AddressPicker(activity, result);
            picker.setSelectedItem(selectedProvince, selectedCity, selectedCounty);
            picker.setOnAddressPickListener(new AddressPicker.OnAddressPickListener() {
                @Override
                public void onAddressPicked(String province, String city, String county) {
//                    Toast.makeText(activity, province + city + county, Toast.LENGTH_LONG).show();
                    sheng.setText(province );
                    shi.setText( city);
                    xian.setText(county);
                    ArrayList<AddressPicker.Province> data = new ArrayList<AddressPicker.Province>();
                    try {
                        String json = AssetsUtils.readText(activity, "city.json");
                        data.addAll(JSON.parseArray(json, AddressPicker.Province.class));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    for (int i=0;i<data.size();i++){
                        if (province.equals(data.get(i).getAreaName())){
                            rongzi_address_sheng_youbian.setText(data.get(i).getAreaId());
                            for (int j=0;j<data.get(i).getCities().size();j++){
                                if (city.equals(data.get(i).getCities().get(j).getAreaName())){
                                    rongzi_address_shi_youbian.setText(data.get(i).getCities().get(j).getAreaId());
                                    for (int k=0;k<data.get(i).getCities().get(j).getCounties().size();k++){
                                        rongzi_address_xian_youbian.setText(data.get(i).getCities().get(j).getCounties().get(k).getAreaId());
                                    }
                                }
                            }
                        }
                    }
                }
            });
            picker.show();

        } else {
            Toast.makeText(activity, "数据初始化失败", Toast.LENGTH_SHORT).show();
        }

    }

}
